package com.icoolkj.learn.juc.future;

import lombok.extern.slf4j.Slf4j;

import java.util.Random;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * @author icoolkj
 * @version 1.0
 * @description
 * @createDate 2025/03/05 14:52
 */
@Slf4j
public class CFDemo4 {
    public static void main(String[] args) {
//        CompletableFuture<Integer> future = CompletableFuture
//                .supplyAsync(() -> {
//                    int number = new Random().nextInt(30);
//                    log.info("第一阶段：{}", number);
//                    return number;
//                })
//                .thenCompose(number ->{
//                    return CompletableFuture.supplyAsync(()->{
//                        int number2 = number *2;
//                        log.info("第二阶段：{}", number2);
//                        return number2;
//                    });
//                 });
//        try {
//            log.info("最终结果：{}", future.get());
//        } catch (InterruptedException e) {
//            throw new RuntimeException(e);
//        } catch (ExecutionException e) {
//            throw new RuntimeException(e);
//        }


        CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> "hello");
        CompletableFuture<String> future1 = future.thenApply(param -> param +" world");
        CompletableFuture<String> future2 = future.thenCompose(param -> CompletableFuture.supplyAsync(() -> param + " world"));
        try {
            log.info("future: {}", future.get());
            log.info("future1: {}", future1.get());
            log.info("future2: {}", future2.get());
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } catch (ExecutionException e) {
            throw new RuntimeException(e);
        }

    }
}
